<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
	<meta name="renderer" content="webkit|ie-comp|ie-stand">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />
	<meta name="keywords" content="zm">
	<meta name="description" content="zm后台管理">
    <title>任务调度</title>
	<link rel="stylesheet" href="common/layui/css/layui.css">
	<link rel="stylesheet" href="common/css/zm.css">
  </head>
<body>
	<div style="padding:15px;">
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
	  <legend>任务列表</legend>
	</fieldset>
 	
 	<div class="layui-form-item">
	    <label class="layui-form-label">任务名称:</label>
	    <div class="layui-input-inline">
	      <input type="text" name="jobName" required  lay-verify="required" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
	    </div>
	    <label class="layui-form-label">任务组名:</label>
	    <div class="layui-input-inline">
	      <input type="text" name="jobGroup" required lay-verify="required" placeholder="请输入任务组名" autocomplete="off" class="layui-input">
	    </div>
	    <div>
	    	<button class="layui-btn layui-btn-primary" onclick="searchData()">查询</button>
	    	<button class="layui-btn layui-btn-primary" onclick="toAdd()">新增</button>
	    	<button class="layui-btn layui-btn-primary" onclick="delMore()">删除</button>
	    </div>
  	</div>
 	
	<div class="layui-form">
	  <table class="layui-table">
	    <colgroup>
	      <col width="10">
	      <col width="30">
	      <col width="30">
	      <col width="200">
	      <col width="30">
	      <col width="30">
	      <col width="100">
	      <col>
	    </colgroup>
	    <thead>
	      <tr>
	        <th><input type="checkbox" id="checkAll" lay-skin="primary" lay-filter="allChoose" style="display:table"></th>
	        <th>任务名称</th>
	        <th>任务组名</th>
	        <th>类名</th>
	        <th>状态</th>
	        <th>触发器状态</th>
	        <th>IP</th>
	        <th>操作</th>
	      </tr> 
	    </thead>
	    <tbody id="jobs">
<!-- 	      <tr>
	        <td><input type="checkbox" name="" lay-skin="primary" style="display:table"></td>
	        <td>贤心</td>
	        <td>汉族</td>
	        <td>1989-10-14</td>
	        <td>正常</td>
	        <td>正常</td>
	        <td>10.10.0.13</td>
	        <td>查看|修改|删除|停止|暂停</td>
	      </tr>
	      <tr>
	        <td><input type="checkbox" name="" lay-skin="primary" style="display:table"></td>
	        <td>张爱玲</td>
	        <td>汉族</td>
	        <td>1920-09-30</td>
	        <td>正常</td>
	        <td>正常</td>
	        <td>10.10.0.13</td>
	        <td>查看|修改|删除|停止|暂停</td>
	      </tr>
	      <tr>
	        <td><input type="checkbox" name="" lay-skin="primary" style="display:table"></td>
	        <td>Helen Keller</td>
	        <td>拉丁美裔</td>
	        <td>1880-06-27</td>
	        <td>正常</td>
	        <td>正常</td>
	        <td>10.10.0.13</td>
	        <td>查看|修改|删除|停止|暂停</td>
	      </tr>
	      <tr>
	        <td><input type="checkbox" name="" lay-skin="primary" style="display:table"></td>
	        <td>岳飞</td>
	        <td>汉族</td>
	        <td>1103-北宋崇宁二年</td>
	        <td>正常</td>
	        <td>正常</td>
	        <td>10.10.0.13</td>
	        <td>查看|修改|删除|停止|暂停</td>
	      </tr>
	      <tr>
	        <td><input type="checkbox" name="" lay-skin="primary" style="display:table"></td>
	        <td>孟子</td>
	        <td>华夏族（汉族）</td>
	        <td>公元前-372年</td>
	        <td>正常 </td>
	        <td>正常</td>
	        <td>10.10.0.13</td>
	        <td>查看|修改|删除|停止|暂停</td>
	      </tr> -->
	    </tbody>
	  </table>
	</div>
	<div id="pageDiv" style="text-align:center;"></div>
	</div>
	<script src="common/layui/layui.js"></script>
	<script type="text/javascript" src="common/js/jquery-3.1.0.js"></script>
	<script>
	var pageNum=1;
	var jobName='';
	var jobGroup='';
	var totalPages=0;
	var pageSize=10;
	//初始化
	$(document).ready(function(){
		loadData();
	});
	
	//loadData
	function loadData(){
		$.ajax({
    		url:'schedule/getScheduleByPage',
    		data:{
    			'pageNum':pageNum,
    			'jobName':jobName,
    			'jobGroup':jobGroup
    		},
    		type:'post',
    		dataType:'json',
    		success:function(result){
    			if(result.status=='200'){
    				var str='';
    				var data=result.data.list;
    				totalPages=result.data.pages;
    				pageSize=result.data.pageSize;
    				for(var i=0;i<data.length;i++){
    					str+='<tr>';
    					str+='<td><input type="checkbox" name="ids" lay-skin="primary" style="display:table" value='+data[i].jobId+'></td>';
    					str+='<td>'+data[i].jobName+'</td>';
    					str+='<td>'+data[i].jobGroup+'</td>';
    					str+='<td>'+data[i].className+'</td>';
    					var status='';
    					if(data[i].status=='0'){
    						status='正常';
    					}else if(data[i].status=='1'){
    						status='停止';
    					}else if(data[i].status=='2'){
    						status='运行';
    					}
    					str+='<td>'+status+'</td>';
    					var triggerStauts='';
    					if(data[i].triggerStauts=='NORMAL'){
    						triggerStauts='正常';
    					}else if(data[i].triggerStauts=='PAUSED'){
    						triggerStauts='暂停';
    					}else if(data[i].triggerStauts=='NONE'){
    						triggerStauts='未运行';
    					}
    					str+='<td>'+triggerStauts+'</td>';
    					str+='<td>'+data[i].ip+'</td>';
    					str+='<td>';
    					//str+='查看|';
    					str+='<a href="jsp/manager/schedule/add.jsp?id='+data[i].jobId+'">修改</a>|';
    					str+='<a href="javaScript:void(0);" onclick="del(\''+data[i].jobId+'\')">删除</a>|';
    					if(data[i].status=='1'){
    						str+='<a href="javascript:void(0);" onclick="start(\''+data[i].jobId+'\')">启动</a>|';
    					}else{
    						str+='<a href="javascript:void(0);" onclick="stop(\''+data[i].jobId+'\')">停止</a>|';
    					}
    					if(data[i].triggerStauts=='PAUSED'){
    						str+='<a id="pause" href="javascript:void(0);" onclick="recovery(\''+data[i].jobId+'\')">恢复</a>';
    					}else{
    						str+='<a id="pause" href="javascript:void(0);" onclick="pause(\''+data[i].jobId+'\')">暂停</a>';
    					}
    					str+='</td>';
    					str+='</tr>';
    				}
    				$("#jobs").html(str);
    				markPage(pageDiv,totalPages,pageSize);
    			}else{
    				
    			}
    		},
    		error:function(result){
    			//alert(result.msg);
    		}
    	});
	}
	//删除
	function del(id){
		$.ajax({
    		url:'schedule/delSchedule',
    		data:{'ids':id},
    		type:'post',
    		dataType:'json',
    		success:function(result){
    			if(result.status=='200'){
    				window.location.href="jsp/manager/schedule/list.jsp";
    			}else{
    				alert(result.msg);
    			}
    		},
    		error:function(result){
    			alert(result.msg);
    		}
    		
    	});
	}
	//删除更多
	function delMore(){
		var ids=getCheckBoxValue();
		$.ajax({
    		url:'schedule/delSchedule',
    		data:{'ids':ids},
    		type:'post',
    		dataType:'json',
    		success:function(result){
    			if(result.status=='200'){
    				window.location.href="jsp/manager/schedule/list.jsp";
    			}else{
    				alert(result.msg);
    			}
    		},
    		error:function(result){
    			alert(result.msg);
    		}
    	});
	}
	//停止
	function stop(id){
		$.ajax({
    		url:'schedule/stopSchedule',
    		data:{'id':id},
    		type:'post',
    		dataType:'json',
    		success:function(result){
    			if(result.status=='200'){
    				window.location.href="jsp/manager/schedule/list.jsp";
    			}else{
    				alert(result.msg);
    			}
    		},
    		error:function(result){
    			alert(result.msg);
    		}
    		
    	});
	}
	//暂停
	function pause(id){
		$.ajax({
    		url:'schedule/pauseSchedule',
    		data:{'id':id},
    		type:'post',
    		dataType:'json',
    		success:function(result){
    			if(result.status=='200'){
    				window.location.href="jsp/manager/schedule/list.jsp";
    			}else{
    				alert(result.msg);
    			}
    		},
    		error:function(result){
    			alert(result.msg);
    		}
    		
    	});
	}
	//恢复
	function recovery(id){
		$.ajax({
    		url:'schedule/recoverySchedule',
    		data:{'id':id},
    		type:'post',
    		dataType:'json',
    		success:function(result){
    			if(result.status=='200'){
    				window.location.href="jsp/manager/schedule/list.jsp";
    			}else{
    				alert(result.msg);
    			}
    		},
    		error:function(result){
    			alert(result.msg);
    		}
    		
    	});
	}
	//启动
	function start(id){
		$.ajax({
    		url:'schedule/startSchedule',
    		data:{'id':id},
    		type:'post',
    		dataType:'json',
    		success:function(result){
    			if(result.status=='200'){
    				window.location.href="jsp/manager/schedule/list.jsp";
    			}else{
    				alert(result.msg);
    			}
    		},
    		error:function(result){
    			alert(result.msg);
    		}
    		
    	});
	}
	//分页
	function markPage(obj,totalPages,pageSize){
		layui.use(['laypage', 'layer'], function(){
		  var laypage = layui.laypage
		  ,layer = layui.layer;
		  laypage({
		    cont: obj
		    ,pages: totalPages //总页数
		    ,groups: 5 //连续显示分页数
		  });
		  //将一段数组分页展示
		  var nums = pageSize; //每页出现的数据量
		  
		  //模拟渲染
		  var render = function(data, curr){
		    var arr = []
		    ,thisData = data.concat().splice(curr*nums-nums, nums);
		    layui.each(thisData, function(index, item){
		      arr.push('<li>'+ item +'</li>');
		    });
		    return arr.join('');
		  };
		});
	}
	//到新增页
	function toAdd(){
		window.location.href="jsp/manager/schedule/add.jsp";
	}
	//查询
	function searchData(){
		loadData();
	}
	//全选和反选
	$("#checkAll").click(function(){   
	    if(this.checked){   
	        $("#jobs :checkbox").prop("checked", true);  
	    }else{   
			$("#jobs :checkbox").prop("checked", false);
	    }  
	    getCheckBoxValue();
	});
	//获取checkbox选中的值
	function getCheckBoxValue(){
		var text='';
		$("input[name=ids]:checked").each(function(index,element) {
            text += ","+$(element).val();  
        }); 
		text=text.substring(1,text.length);
		return text;
	}
	</script>
</body>
</html>